package hu.diploma.android;

import hu.diploma.android.util.Context;
import hu.diploma.common.service.GroupService;
import hu.diploma.common.vos.GroupVo;

import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class LoadThread extends Thread {

	final static int DONE = 1;
	final static int RUNNING = 0;

	int mState;
	int total;
	private final Handler handler;

	public LoadThread(Handler handler) {
		this.handler = handler;

	}

	@Override
	public void run() {
		this.mState = LoadThread.RUNNING;
		this.total = 0;
		while (this.mState == LoadThread.RUNNING) {

			GroupService groupService = Context.getContext().getGroupService();
			Log.e("DEBUG", "getContentResolver : " + Context.getContext().getContentResolver());
			try {
				List<GroupVo> result = groupService.loadAll();
				for (GroupVo groupVo : result) {
					Log.d("DEBUG", groupVo.toString());
				}
				Context.getContext().setGroupVos(result);

			} catch (Exception e) {
				Log.e("DEBUG", e.getStackTrace().toString());
				e.printStackTrace();
			} finally {

				this.mState = LoadThread.DONE;
				Message msg = this.handler.obtainMessage();
				Bundle b = new Bundle();
				b.putInt("state", this.mState);
				msg.setData(b);
				this.handler.sendMessage(msg);
			}
		}

	}

	public void setState(int state) {
		this.mState = state;
	}
}